/*
 * 材质管理API服务
 * @Author: xiehangyun 
 * @Date: 2025-05-16 10:34:03 
 * @Last Modified by: xiehangyun
 * @Last Modified time: 2025-05-16 22:38:41
 */

import Api from "src/api/Api";


export interface MaterialItem {
    id: string;
    name: string;
    thumbnail: string;
    type: 'texture' | 'color' | 'pattern';
    category: string;
    url?: string;          // 材质资源URL
    hexColor?: string;     // 颜色值（HEX格式）
    tileable?: boolean;    // 是否可平铺
    createdAt: string;
    updatedAt: string;
}

export interface MaterialListResponse {
    list: MaterialItem[];
    total: number;
    categories: string[];
}

class MaterialsApi extends Api {

    constructor() {
        super();
    }

    /**
     * 获取材质列表
     * @param params 查询参数
     */
    getMaterialList(params: {
        page?: number;
        pageSize?: number;
        category?: string;
        type?: string;
        keyword?: string;
    }): Promise<{ data: MaterialListResponse }> {
        return this._get('/materials/list', params);
    }

    /**
     * 上传材质文件
     * @param formData 包含文件的FormData
     */
    uploadMaterial(formData: FormData): Promise<{ data: MaterialItem }> {
        return this._post('/materials/upload', formData);
    }

    /**
     * 创建颜色材质
     * @param data 颜色材质数据
     */
    createColorMaterial(data: {
        name: string;
        hexColor: string;
        category: string;
    }): Promise<{ data: MaterialItem }> {
        return this._post('/materials/color', data);
    }

    /**
     * 更新材质信息
     * @param id 材质ID
     * @param data 更新数据
     */
    updateMaterial(id: string, data: Partial<MaterialItem>): Promise<{ data: MaterialItem }> {
        return this._post(`/materials/update/${id}`, data);
    }

    /**
     * 删除材质
     * @param id 材质ID
     */
    deleteMaterial(id: string): Promise<{ data: null }> {
        return this._post(`/materials/delete/${id}`);
    }

    /**
     * 应用材质到设计元素
     * @param designId 设计ID
     * @param elementId 元素ID
     * @param materialId 材质ID
     */
    applyMaterial(
        designId: string,
        elementId: string,
        materialId: string
    ): Promise<{ data: { success: boolean } }> {
        return this._post('/materials/apply', { designId, elementId, materialId });
    }
}

const materialsApi = new MaterialsApi();
export default materialsApi;